import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators, FormControl } from '@angular/forms';
import { Router } from '@angular/router';
import { I18nService } from '../../../core/i18n/i18n.service';
import { RegionNumber } from 'src/app/model';
import { NzMessageService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';


@Component({
  selector: 'app-password-reset',
  templateUrl: './password-reset.component.html',
  styleUrls: ['./password-reset.component.scss'],
})
export class PasswordResetComponent implements OnInit {

  // @Input()
  set current(_current: number) {
    if (_current >= 2) {
        this.resetPassword();
    } else {
      this._current = _current;
    }
  }

  get current(): number {
    return this._current;
  }



  constructor(
    private fb: FormBuilder,
    private router: Router,
    private i18nService: I18nService,
    private api: ApiService
  ) {
    this.resetPwsForm = this.fb.group({
      mobile: [null, [Validators.required]],
      newPwd: [null, [Validators.required]],
      verifyCode: [null, [Validators.required]],
      newConfirmPwd: [null, [Validators.required, this.confirmationValidator]],
    });

  }

  private _current = 0;

  nzSelectedIndex = 0;
  regionNumbers: RegionNumber[] = [];

  resetPwsForm: FormGroup;


  authCodeLable = '发送验证码';
  authCodeLableType = false;


  formNext(num: number) {
  }

  toPage(url: string) {
    this.router.navigateByUrl(url);
  }
  getAuthCodeLable() {
    this.authCodeLableType = true;
    if (this.authCodeLable === '发送验证码' || this.authCodeLable === '重新发送' ) {
      let num = 60;
      this.authCodeLable = '重新发送(' + num + 's)';
      const timmer = setInterval(() => {
          num--;
        this.authCodeLable = '重新发送(' + num + 's)';
      }, 1000);
      setTimeout(() => {
        clearInterval(timmer);
        this.authCodeLableType = false;
        this.authCodeLable = '重新发送';
      }, 20 * 1000);
    }
  }

  changeLang(lang: string) {
    this.i18nService.changeLang(lang);
  }
  sendCodeType = false;
  sendCodeNum = 10;
  _timer;
  toSendCode() {
    if (this.resetPwsForm.value['mobile']) {
      this.api.post('发送手机验证码', { mobile: this.resetPwsForm.value['mobile'], type: 'repw' }).then(res => {
        this.sendCodeNum = res['data']['durationTime'];
        // this.api.msgSuccess(this.msgData['register-code']);
        this.sendCodeType = true;
        this._timer = setInterval(() => {
          if (this.sendCodeNum == 0) {
            this.sendCodeType = false;
            clearInterval(this._timer);
          } else {
            this.sendCodeNum--;
          }
        }, 1000);
      });
    } else {
      // this.api.msgEeeor(this.msgData['register-mail-error']);
    }
  }
  private resetPassword() {
  }

  private confirmationValidator = (control: FormControl): { [ s: string ]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.resetPwsForm.controls.newPwd.value) {
      return { confirm: true, error: true };
    }
  }

  async submit(data) {
    let subData = {mobile:data['mobile'],verifyCode:data['verifyCode'],loginPw:data['newPwd']}
    this.api.post('忘记密码', subData).then(res => {
      this.router.navigate(['pages/login']);
    });
  }

  public successListOk(res, url): void {
    switch (url) {
      case '重置登入密码时(未登入) 发送验证码':
        this.getAuthCodeLable();
        break;
      case '商户重置登入密码(未登陆)':
        this.router.navigate(['pages/login']);
        break;
      }
    }

  ngOnInit() {
  }
}
